package xyz.showbooks.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.dfm.util.JsonResult;
import xyz.dfm.util.WebUtil;
import xyz.dfm.vo.PageResult;
import xyz.dfm.pojo.Comment;
import xyz.showbooks.service.CommentService;


/**
 *
 * 此类提供评论相关接口
 *
 * @author: Dsi
 * @date: 2022/2/18 18:49
 * @description:
 */
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    //根据用户名模糊查询其所属评论
    @GetMapping("/findCommentByUserName/{userName}")
    public JsonResult<Comment> findCommentByUserName(PageResult pageResult){
        JsonResult jsonResult=new JsonResult();
        //排除违法参数
        if (WebUtil.stringIsNull(pageResult.getQuery())){
            return jsonResult.code(201).msg("参数错误" );
        }return jsonResult.code(200).data(commentService.findCommentByUserName(pageResult));
    }
    //根据评论Id修改status
    @PutMapping("/updateStatus/{commentId}/{commentStatus}")
    public JsonResult<Comment> updateStatus(Comment comment){
        if(comment.getCommentId()<0 || comment.getCommentStatus()<0){
            return JsonResult.build(201, "参数错误");
        }return commentService.updateStatus(comment);
    }
    //根据status查找评论
    @GetMapping("/findCommentByStatus/{status}")
    public JsonResult<Comment> findCommentByStatus(PageResult pageResult){
        JsonResult jsonResult=new JsonResult();
        if (WebUtil.intIsNull(pageResult.getQuery())){
            return jsonResult.code(201).msg("参数错误" );
        }return jsonResult.code(200).data(commentService.findCommentByStatus(pageResult));
    }
    //根据书名模糊查询所属评论
    @GetMapping("/findCommentByBooksName/{bookName}")
    public JsonResult<Comment> findCommentByBooksName(PageResult pageResult){
        JsonResult jsonResult=new JsonResult();
        if (WebUtil.stringIsNull(pageResult.getQuery())){
            return jsonResult.code(201).msg("参数错误" );
        }return jsonResult.code(200).data(commentService.findCommentByBooksName(pageResult));
    }
    //...删除评论(修改status即可)

}
